<script lang="ts" setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import * as echarts from 'echarts/core';
  const props = defineProps({
    data1: { type: Object as () => any, required: true },
    data2: { type: Object as () => any, default: [] },
    data3: { type: Object as () => any, default: [] },
    data4: { type: Object as () => any, default: [] },
    data5: { type: Object as () => any, default: [] },
    datax: { type: Object as () => any, required: true },
    title: { type: String, default: '' },
    dataName: { type: Object as () => any, default: [] },
    // max: { type: Number, default: 250000 },
    // min: { type: Number, default: 0 },
  });

  const chartRef = ref<HTMLElement | null>(null);
  let chartInstance: echarts.ECharts | null = null;

  const option = {
    title: {
      text: props.title,
      left: 'center',
      top: '3%',
      textStyle: {
        fontSize: 14,
        fontWeight: 'bold',
      },
    },
    tooltip: {
      trigger: 'axis',
      appendToBody: true, // 关键设置
      axisPointer: {
        type: 'shadow',
      },
    },
    bar: {
      barGap: '30%',
      barCategoryGap: '30%',
    },
    legend: {
      data: props.dataName,
      top: props.title == '' ? '0%' : '12%', // 图例位置
      // right: 5,
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '5%',
      top: '30%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      data: props.datax,
      axisTick: {
        alignWithLabel: true,
      },
    },
    yAxis: {
      // min: props.min,
      // max: props.max,
      type: 'value',
      // interval: 10,
      axisLabel: {
        formatter: '{value}',
      },
      splitLine: {
        show: true,
      },
    },
    series: [
      {
        name: props.dataName[0],
        type: 'bar',
        barGap: 0,
        data: props.data1,
      },
    ],
  };

  const initCharts = () => {
    try {
      option.series[0].data = props.data1 ?? [];
      if (props.data2.length > 0) {
        option.series[1] = {
          name: props.dataName[1],
          barGap: 0,
          type: 'bar',
          data: props.data2,
        };
      } else {
        if (option.series[1]) delete option.series[1];
      }
      if (props.data3.length > 0) {
        option.series[2] = {
          name: props.dataName[2],
          barGap: 0,
          type: 'bar',
          data: props.data3,
        };
      } else {
        if (option.series[2]) delete option.series[2];
      }
      if (props.data4.length > 0) {
        option.series[3] = {
          name: props.dataName[3],
          barGap: 0,
          type: 'bar',
          data: props.data4,
        };
      } else {
        if (option.series[3]) delete option.series[3];
      }
      if (props.data5.length > 0) {
        option.series[4] = {
          name: props.dataName[4],
          barGap: 0,
          type: 'bar',
          data: props.data5,
        };
      } else {
        if (option.series[4]) delete option.series[4];
      }
      option.xAxis.data = props.datax ?? [];
      option.title.text = props.title;
      // option.yAxis.max = props.max;
      // option.yAxis.min = props.min;
      if (chartRef.value && !chartInstance) {
        chartInstance = echarts.init(chartRef.value);
        chartInstance?.setOption(option, true);
      }
    } catch (error) {
      console.error('图表初始化失败:', error);
    }
  };

  const updateChart = () => {
    option.series[0].data = props.data1 ?? [];
    if (props.data2.length > 0) {
      option.series[1] = {
        name: props.dataName[1],
        barGap: 0,
        type: 'bar',
        data: props.data2,
      };
    } else {
      if (option.series[1]) delete option.series[1];
    }
    if (props.data3.length > 0) {
      option.series[2] = {
        name: props.dataName[2],
        barGap: 0,
        type: 'bar',
        data: props.data3,
      };
    } else {
      if (option.series[2]) delete option.series[2];
    }
    if (props.data4.length > 0) {
      option.series[3] = {
        name: props.dataName[3],
        barGap: 0,
        type: 'bar',
        data: props.data4,
      };
    } else {
      if (option.series[3]) delete option.series[3];
    }
    if (props.data5.length > 0) {
      option.series[4] = {
        name: props.dataName[4],
        barGap: 0,
        type: 'bar',
        data: props.data5,
      };
    } else {
      if (option.series[4]) delete option.series[4];
    }
    option.title.text = props.title;
    option.xAxis.data = props.datax ?? [];
    // option.yAxis.max = props.max;
    // option.yAxis.min = props.min;
    if (chartRef.value && chartInstance) {
      chartInstance.setOption(option, true);
    }
  };

  onMounted(() => {
    // 确保在 DOM 加载完成后更新图表
    setTimeout(() => {
      initCharts();
    }, 100);

    // 添加窗口大小变化监听
    window.addEventListener('resize', updateChart);
  });

  onUnmounted(() => {
    // 移除窗口大小变化监听
    window.removeEventListener('resize', updateChart);
    chartInstance?.dispose();
  });

  watch(
    () => [props.datax, props.data1],
    () => updateChart(),
    { deep: true }
  );

  defineExpose({
    updateChart, // 暴露方法到父组件
    chartRef,
  });
</script>
<template>
  <div ref="chartRef" class="chart-container"></div>
</template>

<style scoped>
  .chart-container {
    width: 100%;
    height: 100%;
  }
</style>
